package com.echo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.echo.activity.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineData;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by EchoGe on 2016/6/4.
 */
public class HistoryChartsAdapter extends RecyclerView.Adapter<HistoryChartsAdapter.ViewHolder>{

    private Context mContext;
    private List<LineData> mData;
    private String[] mTitles;
    private int[] mMaxVals;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView text;
        public LineChart chart;

        public ViewHolder(View itemView) {
            super(itemView);
            this.text= (TextView) itemView.findViewById(R.id.history_item_text);
            this.chart= (LineChart) itemView.findViewById(R.id.history_item_chart);
        }

        public void init(float max){
            chart.getAxisLeft().setAxisMaxValue(150f);
            chart.getAxisLeft().setAxisMinValue(0f);
            chart.getAxisLeft().setDrawGridLines(false);
            chart.getXAxis().setDrawGridLines(false);
            chart.setDescription("");

            YAxis leftAxis = chart.getAxisLeft();
            leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
            leftAxis.setTextSize(8f);
            leftAxis.setTextColor(Color.DKGRAY);
            leftAxis.setAxisMaxValue(max);

            XAxis xAxis = chart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setTextSize(8f);
            xAxis.setTextColor(Color.DKGRAY);

            chart.getAxisRight().setEnabled(false);
        }
    }

    public HistoryChartsAdapter(){

    }

    public HistoryChartsAdapter(Context context){
        this.mContext=context;
    }

    public HistoryChartsAdapter(Context context,String[] titles,List<LineData> data){
        this.mContext=context;
        this.mTitles=titles;
        this.mData=data;
    }

    public HistoryChartsAdapter(Context context,String[] titles,List<LineData> data,int[] mMaxVals){
        this.mContext=context;
        this.mTitles=titles;
        this.mData=data;
        this.mMaxVals=mMaxVals;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_chart,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        view.setTag(viewHolder);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.init((float) (mMaxVals[position]*1.1+10));
        holder.text.setText(mTitles[position]);
        holder.chart.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mTitles.length;
        //return datas.size();
    }

}
